<?php
/**
 * 水电导入
 *
 * @author comfiger<liuqiang@wisdudu.com>
 * @version v1.0.0
 *
 **/
namespace Xijie\Controller;
use Think\Controller;

class HydropowerController extends Controller {
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(session('user_id')){ //已登陆
			
		}else{ //未登陆，跳转道登陆界面
			$this->redirect("index/index");
		}
    }
	
	/**
	 * 获取列表
	 **/
	public function index(){
		$page = I('get.page') ? (int)I('get.page') : 1;
		$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
		//获取时间或者类型
		$roomnumber = I('get.roomnumber', '');
		$month = I('get.month', date('Y-m', time()));
		
		if(!empty($roomnumber)){
			$where['roomnumber'] = $roomnumber;
		}
		
		if(!empty($month)){
			$where['month'] = $month;
		}
		
		$count = D('Hydropower')->getHydropowerCount($where);
		//支付列表
		$list = D('Hydropower')->getHydropowerList($page, $where);
		
		$this->assign('list', $list);
		$this->assign('count', $count);
		$this->assign('page', $page);
		$this->assign('years', date('Y'));
		$this->assign('byears', date('Y')-1);
		$this->assign('month', $month.'月');
		//设置查询参数
		if(count($where)){
			$this->assign('param', json_encode($where));
		}
		//判断请求方式
		if(!$ajax){
			$this->display('index');
		}else{
			$this->display('lists', 'utf-8', 'html');
		}
	}
	
	/**
	 * 导入日常费用
	 **/
	public function add(){
		if(IS_GET){
			//$this->display();
		}else{
			ini_set('max_execution_time', '0');
			$allowedExts = array("xlsx", "xls");
			$temp = explode(".", $_FILES["file"]["name"]);
			$extension = end($temp);     // 获取文件后缀名
			if (($_FILES["file"]["size"] < 2048000) && in_array($extension, $allowedExts)){
				if ($_FILES["file"]["error"] > 0){
					echo "Error：: " . $_FILES["file"]["error"] . "<br>";
				}else{
					move_uploaded_file($_FILES["file"]["tmp_name"], "./Public/excel/" . $_FILES["file"]["name"]);
					$filename = "./Public/excel/". $_FILES["file"]["name"];
					//获取excel数据
					$list = D('Excel')->readExcel($filename, $temp, 3);
					
					if(is_array($list) && count($list) >= 1){
						foreach($list as $k=>$ls){
							if(empty($ls['A'])){
								break;
							}
							
							if((empty($ls['G']) || empty($ls['M']))){
								$msg = '编号'. ($ls['A']). '行数据格式错误';
								break;
							}
							
							$data = array(
								'roomnumber' => $ls['B'],
								'before_water' => $ls['D'],
								'water' => $ls['G'],
								'contract_water' => $ls['E'],
								'water_price' => $ls['H'],
								'before_electric' => $ls['J'],
								'electric' => $ls['M'],
								'contract_electric' => $ls['K'],
								'electric_price' => $ls['N'],
								'before_gas' => $ls['P'],
								'gas' => $ls['S'],
								'contract_gas' => $ls['Q'],
								'gas_price' => $ls['T'],
								'month' => $ls['C'],
								'createtime' => time(),
								'admin_nick' => session('nickname')
							);
							
							//判断是否存在数据
							$isCount = D('Hydropower')->getHydropowerCount(array(
								'roomnumber' => $ls['B'],
								'month' => $ls['C']
							));
							
							//存在编辑
							if($isCount){
								$row = D('Hydropower')->editHydropowerInfo(array(
									'roomnumber' => $ls['B'],
									'month' => $ls['C']
								), $data);
							}else{
								$row = D('Hydropower')->addHydropowerInfo($data);
							}
							//判断是否日志
							list($years, $month) = explode('-', $ls['C']);
							$month = $month/1;
							$isLogCount = M('hydropower_log')->where(array(
								'years' => $years,
								'roomnumber' => $ls['B']
							))->count();
							//如果存在
							if($isLogCount){
								M('hydropower_log')->where(array(
									'years' => $years,
									'roomnumber' => $ls['B']
								))->setField(array(
									'water'. $month => $ls['G'],
									'electric'. $month => $ls['M'],
									'gas'. $month => $ls['S']
								));
							}else{
								M('hydropower_log')->data(array(
									'years' => $years,
									'roomnumber' => $ls['B'],
									'water'. $month => $ls['G'],
									'electric'. $month => $ls['M'],
									'gas'. $month => $ls['S']
								))->add();
							}
						}
						
						//删除文件
						unlink($filename);
						//重新加载列表数据
						//$this->index();
						if(empty($msg)){
							echo '上传文件成功';
						}else{
							echo $msg;
						}
					}else{
						echo '上传文件错误';
					}
				}
			}else{
				echo "请选择文件";
			}
		}
	}
	
	/**
	 * 编辑信息
	 **/
	public function edit(){
		if(IS_GET){
			$id = I('get.id', 0);
			$info = D('Hydropower')->getHydropowerInfoById($id);
			
			$this->assign('info', $info);
			$this->display();
		}else{
			$data = I('post.');
			$id = $data['id'];
			unset($data['id']);
			$data['admin_nick'] = session('nickname');
			if(D('Hydropower')->editHydropowerInfo(array('id' => $id), $data) !== false){
				echo 1;
			}else{
				echo 0;
			}
		}
	}
	
	/**
	 * 导出数据
	 **/
	public function export(){
		$roomnumber = I('get.roomnumber', '');
		$years = I('get.years', date('Y'));
		
		if(!empty($roomnumber)){
			$where['roomnumber'] = $roomnumber;
		}
		
		$where['years'] = $years;
		//设置字段名称
		$xlsCell = array(
			array('roomnumber','房间号'),
			array('before_water','上年截转'),
			array('water1','1月'),
			array('water2','2月'),
			array('water3','3月'),
			array('water4','4月'),
			array('water5','5月'),
			array('water6','6月'),
			array('water7','7月'),
			array('water8','8月'),
			array('water9','9月'),
			array('water10','10月'),
			array('water11','11月'),
			array('water12','12月'),
			array('before_electric','上年截转'),
			array('electric1','1月'),
			array('electric2','2月'),
			array('electric3','3月'),
			array('electric4','4月'),
			array('electric5','5月'),
			array('electric6','6月'),
			array('electric7','7月'),
			array('electric8','8月'),
			array('electric9','9月'),
			array('electric10','10月'),
			array('electric11','11月'),
			array('electric12','12月'),
		);
		//获取列表
		$list = D('Hydropower')->getHydropowerAllList($where);

		if(is_array($list)){
			foreach($list as $ls){
				//查询上一年度的最后一个月数据
				$beforeInfo = D('Hydropower')->getHydropowerLogInfo(array(
					'years' => $years-1,
					'roomnumber' => $ls['roomnumber']
				));

				$ls['before_water'] = $beforeInfo['water12'];
				$ls['before_electric'] = $beforeInfo['electric12'];

				$excelData[] = $ls;
			}
		}
		//print("<pre>");
		//print_r($excelData);

		//组装数组
		D('Excel')->exportExcelHydropower($years.'年', $xlsCell, $excelData, $years.'年抄表日志_'.date('Y-m-d H:i'));
	}
	
	/**
	 * 导出模板
	 *
	 **/
	public function exportTemp(){
		$roomnumber = I('get.roomnumber', '');
		$years = I('get.years', date('Y'));
		$month = I('get.month', 1);
		//计算时间
		if(!empty($roomnumber)){
			$where['roomnumber'] = $roomnumber;
		}
		
		$newYear = $years.'-'. str_pad($month, 2, 0, STR_PAD_LEFT);
		$where['hydrop.month'] = $newYear;
		//获取下一月
		$nextMonth = date('Y-m', strtotime("+1 month" , strtotime($newYear.'-01')));
		//设置字段名称
		$xlsCell = array(
			array('number', '编号'),
			array('roomnumber','房间号'),
			array('month','抄表月'),
			array('before_water','上月读数'),
			array('contract_water','新合同读数'),
			array('water_s','损耗'),
			array('water','本月读数'),
			array('water_price','单价'),
			array('water_total','费用'),
			array('before_electric','上月读数'),
			array('contract_electric','新合同读数'),
			array('electric_s','损耗'),
			array('electric','本月读数'),
			array('electric_price','单价'),
			array('electric_total','费用'),
			array('before_gas','上月读数'),
			array('contract_gas','新合同读数'),
			array('gas_s','损耗'),
			array('gas','本月读数'),
			array('gas_price','单价'),
			array('gas_total','费用')
		);
		//获取列表
		$list = D('Hydropower')->getHydropowerAllLogList($where);
		if(is_array($list)){
			foreach($list as $k=>$ls){
				//查询上一年度的最后一个月数据
				$ls['number'] = ($k+1);
				//设置合同数据
				if($nextMonth == date('Y-m', $ls['btime'])){ //新签约合同
					$ls['contract_water'] = $ls['eqment_1'];
					$ls['contract_electric'] = $ls['eqment_2'];
					$ls['contract_gas'] = $ls['eqment_3'];
				}else{
					$ls['contract_water'] = 0;
					$ls['contract_electric'] = 0;
					$ls['contract_gas'] = 0;
				}
				//设置时间
				$ls['month'] = $nextMonth;
				//将查询月的值赋值到上月度数节点
				$ls['before_water'] = $ls['water'];
				$ls['before_electric'] = $ls['electric'];
				$ls['before_gas'] = $ls['gas'];
				//计算损耗
				$ls['water_s'] = empty($ls['contract_water']) ? 0 : ($ls['contract_water'] - $ls['before_water']);
				$ls['electric_s'] = empty($ls['contract_electric']) ? 0 : ($ls['contract_electric'] - $ls['before_electric']);
				$ls['gas_s'] = empty($ls['contract_gas']) ? 0 : ($ls['contract_gas'] - $ls['before_gas']);
				//设置费用为0
				$ls['water_total'] = $ls['gas_total'] = $ls['electric_total'] = 0;
				//设置本月度数为0
				$ls['water'] = $ls['electric'] = $ls['gas'] = 0;
				
				$excelData[] = $ls;
			}
		}
		
		//组装数组
		D('Excel')->exportExcelHydropowerTemp($nextMonth, $xlsCell, $excelData, $nextMonth .'抄表导入模板_'.date('Y-m-d H:i'));
	}
}


   